package day02;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

/*
 *文件名: TreeSetDemo
 *创建者:
 *创建时间:2025/5/20 15:05
 *描述:
 */
public class TreeSetDemo {
    public static void main(String[] args) {
        test0();
       // test1();
        test2();
    }

    public static void test0() {
        //集合只能存储引用数据类型
        TreeSet<Integer> set1 = new TreeSet<>();
        //1 --> 被包装成Integer对象 --》自动装箱
        set1.add(3);
        set1.add(1);
        set1.add(5);
        set1.add(4);
        set1.add(2);
        for (Integer i : set1) {
            System.out.println(i);
        }
        //System.out.println(set1);
    }


    public static void test1() {
        TreeSet<Student> set = new TreeSet<>();

        set.add(new Student("杨过", 28));
        set.add(new Student("阿宝", 19));
        set.add(new Student("何", 20));

        for (Student student : set) {
            System.out.println(student.getName() + "-----" + student.getAge());
        }
    }

    public static void test2() {
//        Comparator<Student> comparator = new Comparator<Student>() {
//
//            @Override
//            public int compare(Student o1, Student o2) {
//                //正序  o1-o2
//                //倒序 o2-o1
//                return o1.getAge() - o2.getAge();
//            }
//        };

        Comparator<Student> comparator =(o1, o2) -> o1.getAge() - o2.getAge();

        TreeSet<Student> set = new TreeSet<>(comparator);
        set.add(new Student("杨过", 28));
        set.add(new Student("阿宝", 19));
        set.add(new Student("何", 20));

        for (Student student : set) {
            System.out.println(student.getName() + "-----" + student.getAge());
        }
    }

    public static int sum(String name,int...a){
        //可变参数必须在形参的最后一个
        int tatol=0;
        for (int i = 0; i < a.length; i++) {
            tatol += a[i];
        }
        return tatol;
    }
}
